//
//  WBHomeViewController.swift
//  WeiBoApp
//
//  Created by James on 2017/6/11.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit
import SDWebImage

//原创微博的 重用标识
fileprivate let cellId = "cellId"

fileprivate let retweetedCellId = "retweetedCellId"


class WBHomeViewController: WBBaseViewController {
    
    //列表试图模型
    fileprivate lazy var listViewModel = WBStatusListViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
//        view.backgroundColor = UIColor.white
        // Do any additional setup after loading the view.
        setupNavTitle()
    }
    
    override func loadData() {
        listViewModel.loadStauts(pullup: self.isPush ){ (isSuccess) in
            self.isPush = false//复位标记
            self.tableView?.reloadData()
            self.refreshControl?.endRefreshing()
        }
    }
    
    @objc fileprivate func showFriend(){
        let vc = WBTestViewController()
//        vc.hidesBottomBarWhenPushed = true//隐藏底部标签栏
        
        navigationController?.pushViewController(vc,animated: true)
    }
    
}

// MARK: -表格数据源方法
extension WBHomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return  listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId) as! WBStatusCell
        
        let viewModel = listViewModel.statusList[indexPath.row]
        
        cell.viewModel = viewModel
        
        return cell
    }
}
 
extension WBHomeViewController{
    
    override func setUpTableView() {
        super.setUpTableView()
        //设置导航栏按钮
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", normalColor: UIColor.gray, highlightColor: UIColor.orange, target: self, action: #selector(showFriend))
       
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: cellId)
        tableView?.register(UINib(nibName: "WBStatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedCellId)
        
        //设置行高
        tableView?.rowHeight = UITableViewAutomaticDimension//自适应
        tableView?.estimatedRowHeight = 300
        
        tableView?.separatorStyle = .none
  
    }
    
    fileprivate func setupNavTitle(){
        let button = WBTitleButton(title: WBNetWorkManger.shared.userAcount.screen_name)
        
        navItem.titleView = button
        
        button.addTarget(self, action: #selector(titleButtonClick(btn:)), for: .touchUpInside)
    }
    
    
    @objc fileprivate func titleButtonClick(btn:UIButton){
        btn.isSelected =  !btn.isSelected
    }
 
}
